package importExport;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;


public class XmlInOut {
	
	
	
	//public Document doc;
	 public  static Document loadDoc(File cFile) {
			Document document = null;
			try {
				DocumentBuilderFactory factory = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder builder;

				builder = factory.newDocumentBuilder();

				document = builder.parse(cFile);
			} catch (SAXParseException spe) {
				System.out.println("\n** Parsing error, line "
						+ spe.getLineNumber() + ", uri " + spe.getSystemId());
				System.out.println("   " + spe.getMessage());
				Exception e = (spe.getException() != null) ? spe.getException()
						: spe;
				e.printStackTrace();
			} catch (SAXException sxe) {
				Exception e = (sxe.getException() != null) ? sxe.getException()
						: sxe;
				e.printStackTrace();
			} catch (ParserConfigurationException pce) {
				pce.printStackTrace();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
			
			return document;
		}
	 public  Document createDoc(File cFile) {
			Document document = null;
			try {
				DocumentBuilderFactory factory = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder builder;

				builder = factory.newDocumentBuilder();

				document = builder.parse(cFile);
			} catch (SAXParseException spe) {
				System.out.println("\n** Parsing error, line "
						+ spe.getLineNumber() + ", uri " + spe.getSystemId());
				System.out.println("   " + spe.getMessage());
				Exception e = (spe.getException() != null) ? spe.getException()
						: spe;
				e.printStackTrace();
			} catch (SAXException sxe) {
				Exception e = (sxe.getException() != null) ? sxe.getException()
						: sxe;
				e.printStackTrace();
			} catch (ParserConfigurationException pce) {
				pce.printStackTrace();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
			
			return document;
		}
	 
	 public String getAttributeValue(Node node,String attributeName){
		NamedNodeMap attributeMap=node.getAttributes();
		return attributeMap.getNamedItem(attributeName).getNodeValue();
	 }
	 public Node getChildByName(Node node,String nodeName){
		 Node cNode=node.getFirstChild();
			while (cNode!=null){
				if (cNode.getNodeName().equals(nodeName)){
					return cNode;
				}
				cNode=cNode.getNextSibling();
			}
			return null;
	 }
	 public Vector<Node> getChildrenByName(Node node,String nodeName){
		 Vector<Node> cNodeList=new Vector<Node>();
		 Node cNode=node.getFirstChild();
			while (cNode!=null){
				if (cNode.getNodeName().equals(nodeName)){
					cNodeList.add(cNode);
				}
				cNode=cNode.getNextSibling();
			}
			return cNodeList;
	 }
	 public Element appendChild(Node parentNode,String nodeName){
		 Document cDoc=parentNode.getOwnerDocument();
			Element cNode=cDoc.createElement(nodeName);
			parentNode.appendChild(cNode);
			return cNode;	
	 }
	 public Element appendChildTextValue(Node parentNode,String nodeName,String textContent){
		Element cNode= appendChild(parentNode,nodeName);
		cNode.setTextContent(textContent);
		return cNode;
	 }
	 
	public  Document createDom(){
		Document cDoc=null;
		DocumentBuilderFactory docBFac;
		DocumentBuilder docBuild;
		try
		{
		docBFac = DocumentBuilderFactory.newInstance();
		docBuild = docBFac.newDocumentBuilder();
		
		cDoc = docBuild.newDocument();
		}
		catch( Exception e )
		{
		System.out.println("Dokument konnte nicht erzeugt werden");
		}
		return cDoc;
	}
	
	
//	This method writes a DOM document to a file
	  public  void writeXmlFile(Document doc, File file) {
		  if (file!=null){
	      try {
	          // Prepare the DOM document for writing
	          Source source = new DOMSource(doc);
	  
	          // Prepare the output file
	         
	          Result result = new StreamResult(file);
	  
	          // Write the DOM document to the file
	          Transformer xformer = TransformerFactory.newInstance().newTransformer();
	          xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
	          xformer.setOutputProperty(OutputKeys.INDENT, "yes");
	          xformer.transform(source, result);
	          
	      } catch (TransformerConfigurationException e) {
	      } catch (TransformerException e) {
	      }}
	      
	  }
	  
	  
	
}
